home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / daten / ispell / interfaces / willy-ttx / spellfix.ttx < prev   
Text File  |  1992-11-19  |  10KB  |  416 lines

  1. /** SpellFix.ttx
  2. *
  3. *   Interface to ISpell to spellfix an entire file. To be called from
  4. *   inside Turbotext. If an argument "all" is given, then "closest root"
  5. *   errors will also be flagged.
  6. *
  7. *   W.G.J. Langeveld, April 1992.
  8. *
  9. *   Hacked by Loren to try to adhere to the new extendedfilecheck
  10. *   method instead of the custom ISpell method that Willy used.
  11. *
  12. *   Loren J. Rittle, Fri Nov 20 01:51:22 1992
  13. *
  14. **/
  15. parse arg allfix
  16.      
  17. options results
  18. options failat 100
  19. /*
  20. *   Allow no interruptions so we can cleanup
  21. */
  22. SIGNAL ON BREAK_C
  23. SIGNAL ON BREAK_D
  24. SIGNAL ON BREAK_E
  25. SIGNAL ON BREAK_F
  26. SIGNAL ON FAILURE
  27. SIGNAL ON HALT
  28. SIGNAL ON SYNTAX
  29. /*
  30. *   Get the screen size and name according to TTX. Set the TTX window
  31. *   size such that there is room for the requester.
  32. */
  33. "getscreeninfo"
  34. info = result
  35. parse var info x x x x x cols rows myspellscreen
  36. /*
  37. *   Check if file has been saved. If not, save it.
  38. */
  39. "getfileinfo"
  40. parse var result x changes x .
  41. if changes = "YES" then "savefile"
  42. /*
  43. *   Change TTX's window size and put up the spellfix requester
  44. */
  45. call SpellRequest(cols, rows, myspellscreen)
  46. /*
  47. *   Start ISpell if it isn't running yet.
  48. */
  49. if ~showlist("p", "IRexxSpell") then do
  50.    call SpellMsg("ISpell not running, starting ISpell")
  51.    address COMMAND "run >nil: <nil: ISpell -R"
  52.    do i = 1 to 50
  53.       call delay(50)
  54.       if showlist("p", "IRexxSpell") then leave i
  55.    end
  56.    if ~showlist("p", "IRexxSpell") then signal ERROR:
  57. end
  58. /*
  59. *   Get the file name, spellcheck the file quickly.
  60. */
  61. "GetFilePath"
  62. name = result
  63. address "IRexxSpell" extendedfilecheck name
  64.      
  65. "SetDisplayLock ON"
  66. spellword. = ""
  67. do i = 1 to ispellresult.count
  68. /*
  69. *   Get location of spell error
  70. */
  71.    spellword.i = ispellresult.i.word
  72.    y = ispellresult.i.line
  73.    x = ispellresult.i.column
  74. /*
  75. *   Get the line it is in
  76. */
  77.    "Move Folds" y 1
  78.    "GetLine"
  79.    line = result
  80. /*
  81. *   If the spell error is somewhere in the line, untab the part
  82. *   before it to find the real column number.
  83. */
  84.    if x ~= 1 then do
  85.       firstpart = substr(line, 1, x - 1)
  86.       firstpart = untab(firstpart)
  87.       x = length(firstpart) + 1
  88.    end
  89. /*
  90. *   Now set a bookmark there.
  91. */
  92.    "Move Folds" y x
  93.    "SetBookmark" 1000 + i
  94. end
  95.      
  96. /*
  97. *   Get number of lines, etc. and move to the start of the file.
  98. */
  99. "GetFileInfo"
  100. parse var result nlines mods name
  101. "MoveSOF"
  102. test = 1
  103.      
  104. /*
  105. *   Loop over the errors (bookmarks).
  106. */
  107. do i = 1 to ispellresult.count
  108.    "MoveBookmark" 1000 + i
  109.      
  110.    item = spellword.i
  111. /*
  112. *   See if the word under the cursor is indeed what we think it is.
  113. *   If not, see if this line *has* a word like that.
  114. */
  115.    "GetWord"
  116.    if result ~= item then do
  117.       "GetLine"
  118.       line = result
  119.       line = untab(line)
  120.       line = translate(line,,
  121. "[]{}(),.;:/<>:?`~!@#$%^&*_+|-=\|"||'"'||'0a'x||'09'x)
  122. /*
  123. *   Loop over the words in the line
  124. */
  125.       place = 0
  126.       n = words(line)
  127.       do j = 1 to n
  128.          if word(line, j) = item then do
  129.             place = wordindex(line, j)
  130.             leave j
  131.          end
  132.       end
  133. /*
  134. *   If the line no longer has the word, we assume it was fixed.
  135. */
  136.       if place = 0 then iterate i
  137. /*
  138. *   Otherwise, we'll position ourselves at that word.
  139. */
  140.       "GetCursorPos"
  141.       parse var result y x .
  142.       "Move folds" y place
  143.    end
  144.      
  145.    address "IRexxSpell" check item
  146. /*
  147. *   Find out what ISpell thought about this one
  148. */
  149.    r = result
  150.    r1 = substr(r, 1, 1)
  151.    string = ""
  152.    alternatives = ""
  153.    select
  154.       when r1 = '*' then do
  155.       end
  156.       when r1 = '&' then do
  157.          string = '"'item||'" not found. Click on Select for alternatives.'
  158.          alternatives = substr(r, 3)
  159.       end
  160.       when r1 = '#' then do
  161.          string = '"'item||'" not found.'
  162.       end
  163.       when r1 = '+' then do
  164.          if allfix ~= "" then do
  165.             string = '"'||item||'" not found. Closest root:' substr(r, 3)
  166.          end
  167.       end
  168.       otherwise string = '"'||item||'":' r
  169.    end
  170. /*
  171. *   If we have a result string, then put the cursor at the offending
  172. *   location and ask the user what to do about it.
  173. */
  174.    if string ~= "" then do
  175.       "SetDisplayLock OFF"
  176.       test = GetNextMsg(item, string, alternatives)
  177.       if test = 0 then leave i
  178.       "SetDisplayLock ON"
  179.    end
  180. end
  181.      
  182. /**
  183. *
  184. *   Clean up
  185. *
  186. **/
  187. BREAK_C:
  188. BREAK_D:
  189. BREAK_E:
  190. BREAK_F:
  191. ERROR:
  192. FAILURE:
  193. HALT:
  194. SYNTAX:
  195.      
  196.    "SetDisplayLock OFF"
  197.    if showlist('p', 'SPELLREQUEST') then do
  198. /*
  199. *   End of document. Give user chance to do more stuff.
  200. */
  201.       if test ~= 0 then do i = 1
  202.          test = GetNextMsg(" ", "All done!", "")
  203.          if test = 0 then leave i
  204.       end
  205.      
  206.       call CloseWindow(SPELLREQUEST)
  207.       "MoveWindow 0 -55"
  208.       "SizeWindow 0 55"
  209.    end
  210.    exit
  211.      
  212.      
  213. /** untab
  214. *
  215. *   Expand tabs in a line.
  216. *
  217. **/
  218. untab: Procedure
  219.    parse arg line
  220.      
  221.    "getprefs tabwidth"
  222.    tabsetting = result
  223.      
  224.    do forever
  225.       n = index(line, '09'x)
  226.       if n = 0 then return line
  227.       nspaces = tabsetting - ((n - 1) // tabsetting)
  228.       line = insert(" ", delstr(line, n, 1), n - 1, nspaces, " ")
  229.    end
  230.      
  231.      
  232. /** SpellRequest
  233. *
  234. *   This program brings up a spell requester
  235. *
  236. *   Add the libraries if they're not there yet.
  237. *
  238. **/
  239. SpellRequest: Procedure
  240. parse arg cols, rows, myspellscreen
  241.      
  242.    if show("l", "rexxarplib.library") = 0 then do
  243.       check = addlib('rexxsupport.library', 0, -30, 0)
  244.       check = addlib('rexxarplib.library',  0, -30, 0)
  245.       check = addlib('rexxmathlib.library', 0, -30, 0)
  246.    end
  247. /*
  248. *   Set the TTX window size such that there is room for the requester.
  249. */
  250.    "sizewindow 0 -55"
  251.    "movewindow 0 55"
  252.      
  253.    mp = OpenPort(MYSPELLPORT)
  254. /*
  255. *   Set up a host. This time, send all messages to MYSPELLPORT.
  256. */
  257.    address AREXX "'x = CreateHost(SPELLREQUEST, MYSPELLPORT, "myspellscreen")'"
  258. /*
  259. *   Wait until it is ready.
  260. */
  261.    do i = 1
  262.       if showlist('p', SPELLREQUEST) ~= 0 then leave
  263.       call delay 30
  264.    end
  265. /*
  266. *   Open the window
  267. */
  268.    idcmp = 'CLOSEWINDOW+GADGETUP+MENUPICK'
  269.    flags = 'WINDOWCLOSE+WINDOWDRAG+WINDOWDEPTH+ACTIVATE'
  270.      
  271.    call OpenWindow(SPELLREQUEST, 0, 0, cols, 55, idcmp, flags, "ISpell Control")
  272. /*
  273. *   Add the gadgets
  274. */
  275.    call AddMenu(SPELLREQUEST, "Commands")
  276.      
  277.    call AddItem(SPELLREQUEST, " Add to Temporary Dictionary",    "ADD", "A", 0)
  278.    call AddItem(SPELLREQUEST, " Go to Next Error",            "IGNORE", "N", 0)
  279.    call AddItem(SPELLREQUEST, " Select Alternatives",         "SELECT", "L", 0)
  280.    call AddItem(SPELLREQUEST, " Search/Replace",                  "SR", "R", 0)
  281.    call AddItem(SPELLREQUEST, " Save Dictionary Additions",     "SAVE", "S", 0)
  282.    call AddItem(SPELLREQUEST, " Exit ISpell",                   "EXIT", "E", 0)
  283.    call AddItem(SPELLREQUEST, " Leave (ISpell stays running)", "CLOSEWINDOW", "Q", 0)
  284.      
  285.    call AddGadget(SPELLREQUEST,  20, 17, 1, " Add ",                      "ADD")
  286.    call AddGadget(SPELLREQUEST,  70, 17, 2, " Next ",                  "IGNORE")
  287.    call AddGadget(SPELLREQUEST, 128, 17, 3, " Select ",                "SELECT")
  288.    call AddGadget(SPELLREQUEST, 222, 17, 4, " S/R ",                       "SR")
  289.    call AddGadget(SPELLREQUEST, 292, 17, 5, " Save Additions ",          "SAVE")
  290.    call AddGadget(SPELLREQUEST, 430, 17, 6, " Exit ISpell ",             "EXIT")
  291.    call AddGadget(SPELLREQUEST, 544, 17, 7, " Leave ",            "CLOSEWINDOW")
  292.      
  293. /*
  294. *   Display a "Checking..." string
  295. */
  296.    call SpellMsg("Checking...")
  297.      
  298.    return 1
  299.      
  300. /** SpellMsg
  301. *
  302. *   Display a line in the spell requester
  303. *
  304. **/
  305. SpellMsg: Procedure
  306.    parse arg line
  307.      
  308.    call WindowText(SPELLREQUEST, "\\"line)
  309.      
  310.    return
  311.      
  312. /** GetNextMsg
  313. *
  314. *   GetNextMsg gets the next message from MYSPELLPORT. Arguments are
  315. *   the word that led to this call and the string we got from ISpell.
  316. *
  317. **/
  318. GetNextMsg: Procedure
  319.    parse arg item, string, alternatives
  320. /*
  321. *   Display the string, after wrapping it if needed.
  322. */
  323.    call SpellMsg(string)
  324. /*
  325. *   Make a list of alternatives
  326. */
  327.    if alternatives ~= "" then do
  328.       wordlist.0 = words(alternatives)
  329.       do i = 1 to wordlist.0
  330.          wordlist.i = word(alternatives, i)
  331.       end
  332.       call SetGadget(SPELLREQUEST, 3, ON)
  333.    end
  334. /*
  335. *   In case the user wants to do a search/replace, set up the find string
  336. */
  337.    "SetPrefs FindString "item
  338.      
  339. restart:
  340. /*
  341. *   Check if there are any more messages. This procedure does not
  342. *   WaitPkt unless there aren't any messages.
  343. */
  344.    do i = 1
  345.       p = getpkt(MYSPELLPORT)
  346.       if p = NULL() then do
  347.          call waitpkt(MYSPELLPORT)
  348.          p = getpkt(MYSPELLPORT)
  349.          if p = NULL() then iterate i
  350.       end
  351.       leave i
  352.    end
  353.    call SetGadget(SPELLREQUEST, 3, OFF)
  354. /*
  355. *   There's a message.
  356. */
  357.    thisarg = getarg(p)
  358.    t = reply(p, 0)
  359. /*
  360. *   Do what needs to be done
  361. */
  362.    select
  363.       when (thisarg = "CLOSEWINDOW") | (thisarg = "EXIT") then do
  364.          if thisarg = "EXIT" then address "IRexxSpell" exit
  365.          return 0
  366.       end
  367.       when thisarg = "IGNORE" then do
  368.          call SpellMsg("Continuing...")
  369.          return 1
  370.       end
  371.       when thisarg = "ADD"    then do
  372.          address "IRexxSpell" quickadd item
  373.          call SpellMsg("Continuing...")
  374.          return 1
  375.       end
  376.       when thisarg = "SAVE"   then do
  377.          address "IRexxSpell" add
  378.          call SpellMsg("Continuing...")
  379.          return 1
  380.       end
  381.       when thisarg = "SR"     then do
  382.          "OpenRequester FindChange"
  383.          signal restart:
  384.       end
  385.       when thisarg = "SELECT" then do
  386.          if show('p', 'QuickSortPort') & (alternatives ~= "") then do
  387. /*
  388. *   Get the screen size and name according to TTX. Set the TTX window
  389. *   size such that there is room for the requester.
  390. */
  391.             "getscreeninfo"
  392.             info = result
  393.             parse var info x x x x x cols rows myspellscreen
  394.      
  395.             call ListRequest(1, wordlist.0, wordlist, selected, 128, 30,  ,
  396.                              280, 170, myspellscreen, SORT)
  397.             if selected ~= "" then do
  398.                "SetPrefs ChangeString "selected
  399.                "GetWord"
  400.                if result = item then  do
  401.                   "ReplaceWord" selected
  402.                   line = "Replaced."
  403.                end
  404.                else do
  405.                   line = "Word under cursor not the same! Not corrected."
  406.                end
  407.                call SpellMsg(line)
  408.             end
  409.          end
  410.          else do
  411.             call SpellMsg("No alternatives found.")
  412.          end
  413.          signal restart:
  414.       end
  415.    end
  416.